package cn.shyouchi.wuguanjia.android.ui.attendance;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import org.joda.time.DateTime;

import java.text.SimpleDateFormat;
import java.util.Map;

import cn.shyouchi.wuguanjia.android.R;
import noman.weekcalendar.decorator.DayDecorator;
import noman.weekcalendar.fragment.WeekFragment;

/**
 * Created by Eenie on 2016/12/14 at 13:27
 * Email: 472279981@qq.com
 * Des:
 */

public class RankDayDecorator implements DayDecorator {

    private Map<String, AttendanceEntity.ResultMessageBean> resultMessageBeanMap;
    private Context context;

    private final int selectedDateColor = Color.parseColor("#1786FF");
    private final int todayDateColor= Color.parseColor("#1786FF");
    private int textColor= Color.GRAY;
    private float textSize = 14;
    private SimpleDateFormat dateFormator = new SimpleDateFormat("yyyy-MM-dd");




    public RankDayDecorator(Context context, Map<String, AttendanceEntity.ResultMessageBean> resultMessageBeanMap) {
        this.context = context;
        this.resultMessageBeanMap = resultMessageBeanMap;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void decorate(View view, TextView dayTextView, DateTime dateTime, DateTime firstDayOfTheWeek, DateTime selectedDateTime) {
        Drawable holoCircle = ContextCompat.getDrawable(context, noman.weekcalendar.R.drawable.holo_circle);
        Drawable solidCircle = ContextCompat.getDrawable(context, noman.weekcalendar.R.drawable.solid_circle);
        holoCircle.setColorFilter(selectedDateColor, PorterDuff.Mode.SRC_ATOP);
        solidCircle.setColorFilter(todayDateColor, PorterDuff.Mode.SRC_ATOP);
        // solidCircle.mutate().setAlpha(200);
        //holoCircle.mutate().setAlpha(200);

        if (firstDayOfTheWeek.getMonthOfYear() < dateTime.getMonthOfYear()
                || firstDayOfTheWeek.getYear() < dateTime.getYear())
            dayTextView.setTextColor(Color.GRAY);

        DateTime calendarStartDate = WeekFragment.CalendarStartDate;

        if (selectedDateTime != null) {
            if (selectedDateTime.toLocalDate().equals(dateTime.toLocalDate())) {
                if (!selectedDateTime.toLocalDate().equals(calendarStartDate.toLocalDate()))

                    dayTextView.setBackground(holoCircle);
            } else {
                dayTextView.setBackground(null);
            }
        }

        String key = dateFormator.format(dateTime.getMillis());
        TextView tvRank = (TextView) view.findViewById(R.id.tv_rank);



        if (resultMessageBeanMap.containsKey(key)) {
            tvRank.setText(resultMessageBeanMap.get(key).getService().getServicesname());
        } else {
            tvRank.setText("");
        }
        if (dateTime.toLocalDate().equals(calendarStartDate.toLocalDate())) {
            dayTextView.setBackground(solidCircle);
            dayTextView.setTextColor(Color.WHITE);
        } else {
            dayTextView.setTextColor(textColor);
        }

        float size = textSize;
        if (size == -1)
            size = dayTextView.getTextSize();
        dayTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);

    }




    public boolean hasData(DateTime dateTime) {
        return resultMessageBeanMap.containsKey(dateFormator.format(dateTime.getMillis()));
    }
    public boolean hasTodayData() {
        return hasData(DateTime.now());
    }



    public AttendanceEntity.ResultMessageBean getData(DateTime dateTime){
        return resultMessageBeanMap.get(dateFormator.format(dateTime.getMillis()));
    }
    public AttendanceEntity.ResultMessageBean getTodayData(){
        return getData(DateTime.now());
    }





}

